// __PUBLIC__/js/page/cms/template/sitetemplatelist
require(['../../common'], function() {
  require(['template.sitetemplatelist'])
})

define('template.sitetemplatelist', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';

  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init();

  function del(selected) {
    var sid = $('#J_hideSid').val()
    $.post(APP + '?app=cms&m=Template&a=deletetpl', {
      ids: selected.id.join(),
      sid: sid
    })
      .done(function(data) {
        if (data == 1) {
          mygrid.del(selected.index)
        } else {
          alert('删除失败')
        }
      })
  }

  $table

  // 删除当前行
  .on('click', '.A_del', function(e) {
    e.preventDefault()
    var current = mygrid.getCurrent(this)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    del(selected)
  })

  // 删除选中行
  $('#J_del').click(function(e) {
    e.preventDefault()
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert(lang.pleaseSelectAData)
      return
    }
    del(selected)
  });

  $(document)
    .on('change', '#J_sitelists', function() {
      var site_id = $(this).val()
      if (site_id > 0) {
        location.href = APP + '?app=cms&m=Template&a=sitetemplatelist&sid=' + site_id
      }else{
    	location.href = APP + '?app=cms&m=Template&a=sitetemplatelist'
      }
    })
    .ready(function() {
      if ($('#J_sitelists').val() > 0) {
        $('.datas-ft').show()
      }
    })
})
